<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Config::orderBy('conf_order','asc')->get();
        foreach($data as $k=>$v){
            switch($v->field_type){
                case 'input':
                    $data[$k]->_html = '<input type="text" class="lg" name="conf_content[]" value="'.$v->conf_content.'">';
                    break;
                case 'textarea':
                    $data[$k]->_html = '<textarea type="text" class="lg" name="conf_content[]">'.$v->conf_content.'</textarea>';
                    break;
                case 'radio':
                    $arr = explode(',',$v->field_value);
                    $str = '';
                    foreach($arr as $m=>$n){
                        $r = explode('|',$n);
                        $c = $v->conf_content==$r[0]?' checked ':'';
                        $str.='<input type="radio" name="conf_content[]"  value="'.$r[0].'"'.$c.'>'.$r[1].'　';
                    }
                    $data[$k]->_html = $str;
                    break;
            }
        }
        return view('admin/config/index',compact('data'));
    }
    public function changeOrder()
    {
        $input = Input::all();
        $config = Config::find($input['conf_id']);
        $config->conf_order = $input['conf_order'];
        $res = $config->update();
        if($res){
            $data = [
                'status' => 0,
                'msg' => '配置项排序更新成功',
            ];
        }else{
            $data = [
                'status' => 1,
                'msg' => '配置项排序更新失败,请重新提交',
            ];
        }
        return $data;
    }

    public function changeContent()
    {
        $input = Input::all();
        $arr = array_combine($input['conf_id'],$input['conf_content']);
        foreach($arr as $k=>$v){
            Config::where('conf_id',$k)->update(['conf_content'=>$v]);
        }
        $this->putFile();
        return back()->with('error','配置修改成功');
    }

    public function putFile()
    {
        $config = Config::pluck('conf_content','conf_name')->all();
        $path = base_path().'/config/web.php';
        $str = '<?php return '.var_export($config,true).';';
       // dd($str);exit;
        file_put_contents($path,$str);
       // echo 123;
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin/config/add');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store()
    {
        $input = Input::except('_token');
        $rules = [
            'conf_name'=>'required',
            'conf_title'=>'required',

        ];
        $message = [
            'conf_name.required'=>'配置项名称不能为空',
            'conf_title.required'=>'配置项标题不能为空',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $rs = Config::create($input);
            if($rs){
                return redirect('admin/config');
            }else{
                return back()->with('error','数据填写失败,请稍后重试');
            }
        }else{
            return back()->withErrors($validator);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($conf_id)
    {
        $field = Config::find($conf_id);
        return view('admin/config/edit',compact('field'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($conf_id)
    {
        $input = Input::except('_token','_method');
        $rs = Config::where('conf_id',$conf_id)->update($input);
        if($rs){
            $this->putFile();
            return redirect('admin/config');
        }else{
            return back()->with('error','配置项更新失败,请稍后重试');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($conf_id)
    {
        $rs = Config::where('conf_id',$conf_id)->delete();
        if($rs){
            $data = [
                'status'=>0,
                'msg'=>'配置项删除成功!'
            ];
            $this->putFile();
        }else{
            $data= [
                'status'=>1,
                'msg'=>'配置项删除失败,请稍后重试!'
            ];
        }
        return $data;
    }
}
